"""
Created on 17-Aug-2009

@author: dexen
"""

InitType = "init_type"
TaskType = "task_type"


class InputUnit(object):
    def __init__(self, task_name, input, avg_exec_time=0):
        self.task_name = task_name
        self.input = input
        self.avg_exec_time = avg_exec_time
        

class OutputUnit(object):
    def __init__(self, task_name, output):
        self.task_name = task_name
        self.output = output
        self.msg = None
        self.is_crashed = False
        self.exec_time = None
        
    def crashed(self, msg):
        self.is_crashed = True
        self.msg = msg


class Task(object):
    def __initialize__(self):
        pass

    def __isvalid__(self, ind):
        pass
    
    def __execute__(self, inds=[]):
        pass

class InitTask(object):
    def __initialize__(self, inds=[]):
        raise NotImplementedError()


def create_Task(classname, inputsize):
    task = TaskInfo()
    task.modulename = classname.__module__
    task.classname = classname.__name__
    task.name = "%s.%s" %(task.modulename, task.classname)
    task.type = TaskType
    task.input_size = inputsize
    return task

def create_InitTask(classname, outputsize):
    task = create_Task(classname, 0)
    task.type = InitType
    return task

class TaskInfo(object):
    def __init__(self, name=None, modulename=None, classname=None, type=None):
        self.name = name
        self.modulename = modulename
        self.classname = classname
        self.type = type
        self.input_size = 0
        self.obj = None # referencing object of Task

    def _get_module(self, modulename):
        return __import__(modulename, fromlist=[""])
    
    def _get_cls(self, modulename, classname):
        module = self._get_module(modulename)
        return getattr(module, classname)
    
    def get_obj(self):
        return self._get_cls(self.modulename, self.classname)()
    
    def __repr__(self):
        return "%s %s %s %s %d" %(self.name, self.modulename, self.classname, 
                                  self.type, self.input_size)

